package cn.edu.hzw.comic.mvp.view

import android.content.Intent
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.view.MenuItem
import android.view.View
import androidx.recyclerview.widget.GridLayoutManager
import androidx.recyclerview.widget.RecyclerView
import cn.edu.hzw.comic.R
import cn.edu.hzw.comic.adapter.BookAdapter
import cn.edu.hzw.comic.adapter.ChapterAdapter
import cn.edu.hzw.comic.mvp.model.Chapter
import cn.edu.hzw.comic.mvp.presenter.ChapterPresenter
import cn.edu.hzw.comic.mvp.view.ChapterActivity.Companion.INTENT_LINK
import com.squareup.picasso.Picasso
import kotlinx.android.synthetic.main.activity_chapter.*
import org.jetbrains.anko.async
import org.jetbrains.anko.uiThread

class ChapterActivity : AppCompatActivity() {
    companion object{
        val INTENT_LINK = "link"
        val INTENT_COVER = "cover"
        val INTENT_TITLE = "title"
        val INTENT_CATEGORY = "category"
    }
    lateinit var link:String
    lateinit var cover:String
    lateinit var title:String
    lateinit var category:String
    lateinit var adapter: ChapterAdapter
    var mDate:List<Chapter> = ArrayList<Chapter>()
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_chapter)
        init()
    }
    private fun init(){
        link = intent.getStringExtra(INTENT_LINK)!!
        cover = intent.getStringExtra(INTENT_COVER)!!
        title = intent.getStringExtra(INTENT_TITLE)!!
        category = intent.getStringExtra(INTENT_CATEGORY)!!
        setSupportActionBar(toolbar)
        supportActionBar?.setDisplayHomeAsUpEnabled(true)
        supportActionBar?.setDisplayShowHomeEnabled(true)
        collapsingToolbar.title = title
        if (cover != ""){
            Picasso.with(this).load(cover).into(imageView)
        }
        swipeRefresh.setColorSchemeResources(R.color.blue,R.color.teal,R.color.green,
        R.color.yellow,R.color.orange,R.color.red,R.color.pink,R.color.purple)
        swipeRefresh.setOnRefreshListener { load() }
        recycleView.layoutManager = GridLayoutManager(this,2) as RecyclerView.LayoutManager?
        adapter = ChapterAdapter{ _: View, i:Int -> jump2Pager(i)}
        recycleView.adapter = adapter
    }

    private fun jump2Pager(position: Int) {
        var intent = Intent(this,PagerActivity::class.java)
        intent.putExtra(PagerActivity.INTENT_LINK,mDate[position].link)
        startActivity(intent)
    }

    override fun onResume() {
        super.onResume()
        swipeRefresh.post{ swipeRefresh.isRefreshing = true }
        load()
    }

    private fun load() {
        async() {
            val data = ChapterPresenter().obtain(link)
            uiThread {
                mDate = data
                adapter.refreshData(data)
                swipeRefresh.isRefreshing = false
            }
        }
    }

    override fun onOptionsItemSelected(item: MenuItem): Boolean {
        val id = item.itemId
        if (id == R.id.home){
            onBackPressed()
            return true
        }
        return super.onOptionsItemSelected(item)
    }
}